package airacrobats.control;

import airacrobats.game.object.Airplane;
import airacrobats.io.data.MovementData;
import java.util.List;

/**
 * This class controls an airplane.
 */
public abstract class Controller
{ 
    /**
     * Airplane.
     */
    protected Airplane airplane;
    /**
     * Movements linked to this controller.
     */
    protected List<MovementData> movements;
    /**
     * The current movement.
     */
    protected MovementData currentMovement;

    /**
     * @param airplane The airplane.
     * @param movements The movements of the route.
     */
    public Controller(Airplane airplane, List<MovementData> movements)
    {
        this.airplane = airplane;
        this.movements = movements;
    }

    /**
     * Updates the controller.
     * @param time The current time.
     * @return The score generated by this controller.
     */
    public int update(long time, long deltaTime)
    {      
        // Note: this is done to filter movements faster than the loop tick.
        while (!movements.isEmpty() && time >= movements.get(0).getTime())
        {
            currentMovement = movements.get(0);
            movements.remove(0);
        }

        return doUpdate(deltaTime);
    }

    /**
     * @return The controller airplane.
     */
    public Airplane getAirplane()
    {
        return airplane;
    }

    /**
     * Calculates the score and returns it.
     * @param deltaTime The delta time.
     * @return The score.
     */
    protected abstract int doUpdate(long deltaTime);
}
